/*
 * Copyright (c) 2020, 2023, 3044483124@qq.com All rights reserved.
 *
 */
package cn.itcast;

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;

/**
 * <p>Project: JavaStudy - FileCopy
 * <p>Powered by jiangbo On 2023-01-03
 * 15:20:28
 * <p>Created by IntelliJ IDEA
 *
 * @author jiangbo [3044483124@qq.com]
 * @version 1.0
 * @since 8/17
 */
public class FileCopy {
        public static void main(String[] args){

//            File file = new File("test.png");
//            try {
//                if (file.isFile() && file.exists()) {
//                    file.delete();
//                    System.out.println("已删除原有文件");
//                } else {
//                    System.out.println("开始创建复制");
//                }
//            } catch (Exception e) {
//                System.out.println("错误");
//
//            }
            //C:\Users\30444\Desktop\截图\信息.png 复制到项目目录下.
            String p = "C:\\Users\\30444\\Desktop\\截图\\wang_001.png";
            try (
                    var fis = new FileInputStream(p);
                    var fos = new FileOutputStream("test.png")
            ) {
//fos.write(fis.readAllBytes());
//int b = 0;
//while((b = fis.read())!=-1){
// fos.write(b);
//}
//推荐使用如下复制文件
//byte[] buf = new byte[10240];
//int len = 0;
//while ((len = fis.read(buf)) != -1) {
// fos.write(buf, 0, len);
//}
                fis.transferTo(fos);
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
          }
}

